package com.hxht.cmp.service.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.entity.Device;
import com.hxht.cmp.mapper.DeviceMapper;
import com.hxht.cmp.pojo.DevicePojo;
import com.hxht.cmp.service.DeviceService;
import com.hxht.cmp.utils.MD5Utils;
import com.hxht.cmp.utils.http.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DeviceServiceImpl implements DeviceService {
    private final Logger log = LoggerFactory.getLogger(DeviceServiceImpl.class);
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public JsonArray getDevStatus(String courtUid) {
        Court court = Constants.COURT_MAP.get(courtUid);
        String url = "http://" + court.getServiceIp() + ":" + Constants.SERVER_PORT + "/getCourtRoomStatus";
        log.info("http协议获取庭审主机信息" + url);
        HttpClientRequest request = new HttpClientRequest(url, RequestMethod.GET);
        Long timestamp = System.currentTimeMillis();
        request.addHeader("Authorization", "basic " + MD5Utils.code(Constants.CLIENT_ID + timestamp + Constants.CLIENT_SCERET));
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("appId", Constants.CLIENT_ID);
        jsonObject.addProperty("timestamp", timestamp);
//        request.setJsonObject(jsonObject);
        HttpClientResponse response = HttpClientUtil.doRequest(request);
        if (null != response && response.getCode() == 200) {
            String responseText = response.getResponseText();
            if (!StringUtils.isBlank(responseText)) {
                JsonParser jsonParser = new JsonParser();
                JsonElement jsonElement = jsonParser.parse(responseText);
                JsonArray jsonArray = jsonElement.getAsJsonArray();
                return jsonArray;
            } else {
                return null;
            }
        } else {
            log.info("未获取到远程数据");
            return null;
        }
    }

    @Override
    public List<DevicePojo> selectDeviceByCourtUid(String courtUid, String courtroomName) {
        return deviceMapper.selectDeviceByCourtUid(courtUid, courtroomName);
    }

    @Override
    public Map<String, Object> countDeviceByCourtUid(String courtUid, String courtroomName, int page, int pageSize) {
        int pageStart = (page - 1) * pageSize;
        int count = deviceMapper.countDeviceByCourtUid(courtUid,courtroomName);
        List<DevicePojo> list = deviceMapper.selectDeviceByCourtUidForPage(courtUid,courtroomName,pageStart,pageSize);
        Map<String,Object> result = new HashMap<>();
        result.put("count",count);
        result.put("data",list);
        return result;
    }

    @Override
    public int addDevice(Device device) {
        device.setCourtName(Constants.COURT_MAP.get(device.getCourtUid()).getName());
        device.setCourtRoomName(Constants.COURTROOM_MAP.get(device.getCourtRoomUid()).getName());
        device.setLastUpdated(new Date());
        device.setDateCreated(new Date());
        return deviceMapper.insertSelective(device);
    }

    @Override
    public int updateDevice(Device device) {
        device.setCourtName(Constants.COURT_MAP.get(device.getCourtUid()).getName());
        device.setCourtRoomName(Constants.COURTROOM_MAP.get(device.getCourtRoomUid()).getName());
        device.setLastUpdated(new Date());
        return deviceMapper.updateByPrimaryKeySelective(device);
    }

    @Override
    public Device selectDeviceByPrimaryKey(long id) {
        return deviceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteDeviceByPrimaryKey(long id) {
        return deviceMapper.deleteByPrimaryKey(id);
    }
}

